class BucketsController < ApplicationController

  skip_before_filter :verify_authenticity_token, :only => :auto_complete_for_setting_name

  def index
    @buckets = Bucket.all
  end

  def show
    @bucket = Bucket.find(params[:id])
    @axes = Axis.all
  end

  def new
    @bucket = Bucket.new
  end

  def create
    @bucket = Bucket.new(params[:bucket])
    if @bucket.save
      flash[:notice] = "bucket created"
      redirect_to "/"
    else
      render :action => 'new'
    end
  end

  def destroy
    Bucket.destroy(params[:id])
    flash[:notice] = "bucket deleted"
    redirect_to "/"    
  end

  def auto_complete_for_setting_name
    query = params[:val]
    return if query.empty? or query.nil?
    @settings = Setting.with_name_like(query)
    render :action => 'auto_complete_for_setting_name', :content_type => 'application/json', :layout => false
  end

  def settings_list
    @bucket = Bucket.find(params[:id])
    render :partial => 'settings_list', :locals => { :settings => @bucket.settings }
  end

  def update
    @bucket = Bucket.find(params[:id])
    if @bucket.update_attributes(params[:bucket])
      flash[:notice] = "bucket updated"
      redirect_to bucket_url(@bucket)
    else
      render :action => 'edit'
    end
  end

  def show_context
    @bucket = Bucket.find_by_name(params[:name])
    axes = params[:axes].reject { |a| a == "*" }
    @resolved_settings = @bucket.resolve_settings_for axes
    render :layout => false
  end

end
